import { SETTYPES, SETOBJ, SETLIST } from '../mutation-type'
import { post } from '@/utils/axios'

export default {
  namespaced: true, // 命名空间
  state: {
    // 表示是从详情直接进入购买单个商品
    obj: null,
    // 表示购物车结算,可能是多个商品
    list: null,
    // 表示具体哪个类型
    types: ''
  },
  mutations: {
    [SETTYPES] (state, payload) {
      state.types = payload
    },
    [SETOBJ] (state, payload) {
      state.obj = payload
    },
    [SETLIST] (state, payload) {
      state.list = payload
    }
  },
  actions: {
    getCartListAction(context, payload) {
      post({
        url: '/hybrid/uni/cart'
      }).then(res => {
        const { result, status } = res
        if (status >= 0) {
          context.commit('SETCARTLIST', result)
        }
      })
    },
    deleteSelectCart(context, payload) {
      post({
        url: '/hybrid/uni/place-an-order',
        data: payload
      }).then(res => {
        context.dispatch('getCartListAction')
      })
    }
  }
}
